package myJs.specialMember.sampleList

import myJs.CpLayer
import myJs.Implicits.JDJAn
import myJs.cps.BootstrapTableCp.Column
import myJs.cps.{BootstrapTableCp, InputInlineCp, Style}
import myJs.myPkg.{MyFetch, SwalOptions, swal}
import myJs.tool.Pojo.{DateSearch, TextSearch}
import myJs.tool.Tool
import slinky.core.FunctionalComponent
import slinky.core.annotations.react
import slinky.core.facade.Hooks.{useEffect, useState}
import slinky.web.html._

import scala.collection.immutable.SeqMap
import scala.scalajs.js
import myJs.Implicits._
import myJs.api.Api
import myJs.common.LabModalCp
import myJs.cps.project.{BasicInfoModalCp, FreezerInfoModalCp, QrcodeModalCp}
import myJs.specialMember.viewSample.ViewSample
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Sample.{FreezerInfo, TestData}
import typings.csstype.csstypeStrings
import upickle.default._

/**
 * Created by yz on 21/1/2022
 */
@react object Cp {

  val curPage = SampleList
  type Props = Unit

  val component = FunctionalComponent[Props] { props =>

    val (datas, setDatas) = useState(List.empty[ujson.Obj])
    val (sampleId, setSampleId) = useState("")
    val (basicInfoShow, setBasicInfoShow) = useState(false)
    val (freezerInfoShow, setFreezerInfoShow) = useState(false)
    val (testInfoShow, setTestInfoShow) = useState(false)
    val (qrcodeId, setQrcodeId) = useState("")
    val (qrcodeShow, setQrcodeShow) = useState(false)
    val (labShow, setLabShow) = useState(false)

    object FTool {

      def labShowChange = (v: Boolean) => {
        setLabShow(v)
      }

      def labShow(curSampleId: String) = () => {
        setSampleId(curSampleId)
        setLabShow(true)
      }

      def refreshTableB = () => {
        Api.sample.all.map { response =>
          setDatas(response)
        }
      }

      def basicInfoShow(curSampleId: String) = () => {
        setSampleId(curSampleId)
        setBasicInfoShow(true)
      }

      def freezerInfoShow(curSampleId: String) = () => {
        setSampleId(curSampleId)
        setFreezerInfoShow(true)
      }

      def testInfoShow(curSampleId: String) = () => {
        setSampleId(curSampleId)
        setTestInfoShow(true)
      }

      def qrcodeShowChange = (v: Boolean) => {
        setQrcodeShow(v)
      }

      def showQrcode(idStr: String) = () => {
        setQrcodeId(idStr)
        setQrcodeShow(true)
      }

      def tbFmt(field: String) = (v: ujson.Value, row: ujson.Obj) => {
        field match {
          case "id" =>
            span(
              a(href := s"${ViewSample.absolutePath}?id=${row("id").str}", s"${v.str}"),
              " ",
              i(className := "fa fa-qrcode", style := Style(cursor = csstypeStrings.pointer),
                onClick := FTool.showQrcode(v.str))
            )()
          case "basicInfo" =>
            button(className := "btn-link get_group_info", title := "查看",
              onClick := FTool.basicInfoShow(row("id").str),
              i(className := "fa fa-search-plus"))()
          case "freezerInfo" =>
            Tool.freezerInfoFmt(v = v, row = row, freezerInfoShow = FTool.freezerInfoShow)
          case "lab" =>
            Tool.labFmt(v = v, row = row, labShow = FTool.labShow)
          case "tests" =>
            val testInfos = v.transform(reader[List[TestData]])
            if (testInfos.nonEmpty) {
              button(className := "btn-link get_group_info", title := "查看",
                onClick := FTool.testInfoShow(row("id").str),
                i(className := "fa fa-search-plus"))()
            } else {
              span("无")()
            }
          case _ => span(v.myStr)()
        }
      }

      def comVFmt(fileTitle: String) = (v: ujson.Value, row: ujson.Obj) => {
        fileTitle match {
          case _ => v.myStr
        }
      }

      def basicInfoShowChange = (v: Boolean) => {
        setBasicInfoShow(v)
      }

      def testInfoShowChange = (v: Boolean) => {
        setTestInfoShow(v)
      }

      def freezerInfoShowChange = (v: Boolean) => {
        setFreezerInfoShow(v)
      }

    }

    useEffect(() => {
      FTool.refreshTableB()
    }, List()
    )

    val map = SeqMap("id" -> "样本编号", "basicInfo" -> "基本信息", "freezerInfo" -> "冰箱信息", "lab" -> "实验信息",
      "tests" -> "测试信息",
      "updateTime" -> "更新时间")

    val basicColumns = map.map { case (k, v) =>
      val searchKind = k match {
        case "createTime" => DateSearch
        case _ => TextSearch
      }
      val fmt = FTool.tbFmt(k)
      val comVF = FTool.comVFmt(v)
      Column(field = k, title = v, operable = true, searchKind = searchKind, formatter = fmt, comVF = comVF)
    }.toList
    val columns = basicColumns

    CpLayer(title = curPage.titleStr)(
      div(className := "page-content",
        div(className := "row-fluid",
          div(className := "row",
            div(className := "col-md-12 col-sm-12",
              div(className := "portlet light",
                div(className := "portlet-title",
                  div(className := "caption", s"${curPage.titleStr}")
                ),
                div(className := "portlet-body",

                  BootstrapTableCp(columns = columns, datas = datas, pageSize = 10),

                  BasicInfoModalCp(show = basicInfoShow, showChange = FTool.basicInfoShowChange, sampleId = sampleId),
                  FreezerInfoModalCp(show = freezerInfoShow, showChange = FTool.freezerInfoShowChange,
                    sampleId = sampleId),
                  LabModalCp(show = labShow, showChange = FTool.labShowChange,
                    sampleId = sampleId, labFileShow = false),
                  TestInfoWithDetailModalCp(show = testInfoShow, showChange = FTool.testInfoShowChange,
                    sampleId = sampleId),
                  QrcodeModalCp(show = qrcodeShow, showChange = FTool.qrcodeShowChange,
                    sampleId = qrcodeId)

                ),
              )
            )
          )
        )
      )
    )

  }


}
